package com.chen.pfm.provider;

import com.chen.pfm.ResourceTable;
import com.chen.pfm.entity.DisplayBill;
import com.chen.pfm.utils.DateUtils;
import com.chen.pfm.utils.MyUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import java.util.List;

public class DisplayBillProvider extends BaseItemProvider {
    List<DisplayBill> list;
    AbilitySlice slice;

    public DisplayBillProvider(List<DisplayBill> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }
     class ViewHolder{
         Image image;
         Text cname;
         Text aremark;
         Text amoney;
         Text recordtime;

         public ViewHolder(Component component) {
             image =(Image) component.findComponentById(ResourceTable.Id_item_image);
             cname=(Text) component.findComponentById(ResourceTable.Id_item_cname);
             aremark = (Text) component.findComponentById(ResourceTable.Id_item_aremark);
             amoney= (Text) component.findComponentById(ResourceTable.Id_item_amoney);
             recordtime=(Text) component.findComponentById(ResourceTable.Id_title);
         }
     }
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder=null;
        DisplayBill displayBill = list.get(i);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_template_items_account, null, false);
            holder=new ViewHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder=(ViewHolder) cpt.getTag();
        }
        //设置属性返回

        holder.image.setPixelMap(displayBill.getIcon());
        holder.cname.setText(displayBill.getCname());
        holder.aremark.setText(displayBill.getAremark());
        holder.recordtime.setText(DateUtils.getChineseFormatFromStr(displayBill.getRecordTime()));
        if(displayBill.getType1()==2){//收入绿色
            holder.amoney.setTextColor(new Color(Color.getIntColor("#FF47AB93")));
        }else  if(displayBill.getType1()==1){//支出红色
            holder.amoney.setTextColor(new Color(Color.getIntColor("#ff383b")));
        }else{//资产蓝色
            holder.amoney.setTextColor(new Color(Color.getIntColor("#1296db")));
        }
        holder.amoney.setText(MyUtil.getStrBalance(displayBill.getAmoney()));
        return cpt;
    }
}
